package com.ucode.hrm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ucode.hrm.mapper.HrmEmployeeContractMapper;
import com.ucode.model.hrm.HrmEmployeeContract;
import com.ucode.model.hrm.enums.HrmSysResultCodeEnum;
import com.ucode.springboot.starter.mybatis.core.base.BaseService;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工合同Service
 * @Author: liliang
 * @Date: 2025/6/7 14:20
 * @Version: 1.0
 */
public interface HrmEmployeeContractService extends BaseService<Long, HrmEmployeeContract, HrmEmployeeContractMapper> {

    /**
     * 三方合同生成
     * @param id 合同ID
     * @param employeeId 员工ID
     * @return 在线合同签署地址
     */
    public String generateThreeContract(Long id,Long employeeId);

    /**
     * 签约成功通知
     * @param transactionId
     * @param contractId
     * @param downloadUrl
     * @param viewpdfUrl
     */
    public void signNotify(String transactionId,Long contractId,String downloadUrl,String viewpdfUrl);

    /**
     * 批量更新可见状态
     * @param ids
     * @param visible
     * @return
     */
    public int updateVisibleByIds(Collection<Long> ids, Boolean visible);

    default Map<Long, HrmEmployeeContract> getMap(Collection<Long> ids) {
        return CollUtil.isEmpty(ids) ? new HashMap() : CollectionUtils.convertMap(this.selectByPrimaryKeys(ids), HrmEmployeeContract::getId);
    }

    /**
     * 查询员工合同列表
     * @param employeeId
     * @return
     */
    default List<HrmEmployeeContract> queryListByEmployeeId(Long employeeId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("employeeId", employeeId);
        condition.put("deltag", false);
        return this.queryObjectForList(condition, 0);
    }

    /**
     * 查询员工合同列表
     * @param employeeId
     * @param visible
     * @return
     */
    default List<HrmEmployeeContract> queryListByEmployeeId(Long employeeId,Boolean  visible) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("employeeId", employeeId);
        condition.put("visible", visible);
        condition.put("deltag", false);
        return this.queryObjectForList(condition, 0);
    }

    /**
     * 验证是否合法并返回
     * @param id
     * @return
     */
    default HrmEmployeeContract validate(Long id){
        if(ObjectUtil.isNull(id) )
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);
        //验证数据是否存在
        HrmEmployeeContract brand = this.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(brand) || brand.getDeltag()){
            throw new UcodeServiceException(HrmSysResultCodeEnum.EMPLOYEE_CONTRACT_NOT_EXISTS);
        }
        return brand;
    }

}
